<template>
    <el-dialog @open="open" v-model="isOpen" :title="props.openType==='add'?'添加仓库信息':'修改仓库信息'" @close="close" style="width: 30%">
        <el-form ref="dataForm" :rules="rules" :model="form" label-width="100px" align="center">
            <el-form-item label="仓库名" prop="name">
                <el-input  v-model="form.name"/>
            </el-form-item>
            <el-form-item label="仓库地址" prop="address">
                <el-input v-model="form.address"/>
            </el-form-item>
            <el-form-item label="区域编码" prop="areacode">
                <el-input v-model="form.areacode"/>
            </el-form-item>
            <el-form-item>
                <el-button v-show='props.openType==="add"' type="primary" activity  @click="addWmsWareInfo">添加</el-button>
                <el-button v-show="props.openType==='edit'" type="success" activity  @click="updateWmsWareInfo">更新</el-button>
                <el-button @click="close" type="success" activity >取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
    import {ref,reactive} from 'vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    const  emits = defineEmits(['close','reload'])

    let dataForm = ref(null)
    let props = defineProps(['openType','id'])
    let form = reactive({})
    let isOpen = ref(false)
    defineExpose({isOpen})

    const rules = reactive({
            name: [{required: true,message: '请输入仓库名', trigger: 'blur'},],
            address: [{required: true,message: '请输入仓库地址', trigger: 'blur'},],
            areacode: [{required: true,message: '请输入区域编码', trigger: 'blur'},],
    })
    function open(){
        if(props.openType==='edit'){
            request.get(`/api/wmsWareInfo/getWmsWareInfoById/${props.id}`).then(resp => {
                for(let key in resp){
                    form[key] = resp[key]
                }
            })
        }else if(props.openType==="add"){
            for(let key in form){
                delete form[key]
            }
        }
    }

    function close(){
        emits("close")
        dataForm.value.resetFields()//重置表单校验
    }

    function addWmsWareInfo(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/wmsWareInfo/add",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '添加成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
    function updateWmsWareInfo(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/wmsWareInfo/update",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '修改成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
</script>

<style scoped>

</style>